﻿Partial Class WebForm_SearchRoleUserMappingByRole
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Visible = True
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Me.Show(Me.RoleID)
        'Me.SearchUserControl1.Show(Me.RoleID)
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        If Me.DeleteData(e.RowIndex) = True Then
            Me.Show(Me.RoleID)
        End If
    End Sub

    Protected Sub SearchUserControl1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchUserControl1.Saving
        Me.SaveData()
        Me.Show(Me.RoleID)
    End Sub
End Class

Partial Class WebForm_SearchRoleUserMappingByRole
    Private Sub Show(ByVal roleID As String)
        Me.RoleID = roleID
        Me.BindData()
        Me.BindGV()

        Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Public Sub Show(ByVal roleID As String, ByVal owner As My.MyUserControl)
        Me.OwnerControl = owner
        Me.Show(roleID)
    End Sub

    Public Sub ShowReadOnly(ByVal roleID As String)
        Me.Show(roleID)

        Me.btnAdd.Visible = False
        Me.gv.Columns(0).Visible = False
    End Sub

    Private Sub BindData()

        'If clsAuth.GetTableAuth("RoleUserMapping", "Add") = False Then
        If Me.CanAdd = False Then
            Me.btnAdd.Enabled = False
        Else
            Me.btnAdd.Enabled = True
        End If

    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("select distinct ru.* ")
        sql.AppendFormat(", u.UserName ")
        sql.AppendFormat("from RoleUserMapping ru ")
        sql.AppendFormat("inner join [User] u on ru.UserID = u.UserID ")
        'sql.AppendFormat("inner join RoleOrganizationMappingView ro on u.OrganizationID = ro.OrganizationID ")
        sql.AppendFormat("where ru.RoleID = '{0}' ", Me.RoleID)
        'sql.AppendFormat("and ro.RoleID in ({0}) and ro.AuthName in ('Add','Delete','Admin') ", clsAuth.GetRoleList())
        Me.gv.DataSource = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt")
        Me.gv.DataBind()

        Return

        ' 判斷是否有刪除權限
        sql.Length = 0
        sql.AppendFormat("select ru.* ")
        sql.AppendFormat(", u.UserName, ro.AuthName ")
        sql.AppendFormat("from RoleUserMapping ru ")
        sql.AppendFormat("inner join [User] u on ru.UserID = u.UserID ")
        'sql.AppendFormat("inner join RoleOrganizationMappingView ro on u.OrganizationID = ro.OrganizationID ")
        sql.AppendFormat("where ru.RoleID = '{0}' ", Me.RoleID)
        'sql.AppendFormat("and ro.RoleID in ({0}) and ro.AuthName in ('Add','Delete','Admin') ", clsAuth.GetRoleList())

        Dim dt As Data.DataTable = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0)

        For Each row As GridViewRow In Me.gv.Rows
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            dt.DefaultView.RowFilter = String.Format("UserID = '{0}' and AuthName in ('Delete','Admin')", key("UserID"))

            If dt.DefaultView.Count = 0 Then
                row.Cells(0).Enabled = False
            End If
        Next
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            For Each userID As String In Me.SearchUserControl1.SelectedUserList
                sql.Length = 0
                sql.AppendFormat("insert RoleUserMapping (RoleID, UserID) select ")
                sql.AppendFormat(" '{0}' ", Me.RoleID)
                sql.AppendFormat(",'{0}' ", userID)
                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            Next
            scope.Complete()
        End Using

        Return True
    End Function

    Private Function DeleteData(ByVal rowIndex As Integer) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim row As GridViewRow = Me.gv.Rows(rowIndex)
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("delete RoleUserMapping where ")
            sql.AppendFormat(" RoleID='{0}' ", key("RoleID"))
            sql.AppendFormat("and UserID='{0}' ", key("UserID"))
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            scope.Complete()
        End Using

        Return True
    End Function

    Public Property RoleID() As String
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As String)
            ViewState("RoleID") = value
        End Set
    End Property
End Class